package com.yami.shop.api.controller;

import com.yami.shop.bean.app.dto.ResourcesInfoDto;
import com.yami.shop.bean.model.AttachFile;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.HuaweiOcr;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.service.AttachFileService;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传 controller
 *
 * @author lgh
 */

@RestController
@RequestMapping("/p/file")
public class FileController {

    @Autowired
    private AttachFileService attachFileService;

    @Autowired
    private ShopConfig shopConfig;

    @PostMapping("/upload")
    @Operation(summary =  "文件上传接口", description = "上传文件，返回文件路径与域名")
    public ServerResponseEntity<ResourcesInfoDto> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return ServerResponseEntity.success();
        }
        String fileName = attachFileService.uploadFile(file.getBytes(), file.getOriginalFilename());
        String resourcesUrl = shopConfig.getDomain().getResourcesDomainName() + "/";
        ResourcesInfoDto resourcesInfoDto = new ResourcesInfoDto();
        resourcesInfoDto.setResourcesUrl(resourcesUrl);
        resourcesInfoDto.setFilePath(fileName);
        return ServerResponseEntity.success(resourcesInfoDto);
    }

    @PostMapping("/uploadImFile")
    @Operation(summary =  "聊天文件上传接口", description = "上传文件，返回文件路径与域名")
    public ServerResponseEntity<ResourcesInfoDto> uploadImFile(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return ServerResponseEntity.success();
        }
        String fileName = attachFileService.uploadImFile(file.getBytes(), file.getOriginalFilename());
        String resourcesUrl = shopConfig.getDomain().getResourcesDomainName() + "/";
        ResourcesInfoDto resourcesInfoDto = new ResourcesInfoDto();
        resourcesInfoDto.setResourcesUrl(resourcesUrl);
        resourcesInfoDto.setFilePath(fileName);
        return ServerResponseEntity.success(resourcesInfoDto);
    }

    @GetMapping("/get_file_by_id")
    @Operation(summary =  "根据文件id获取文件信息")
    public ServerResponseEntity<AttachFile> getFileById(@RequestParam("fileId") Long fileId) {
        AttachFile attachFile = attachFileService.getById(fileId);
        return ServerResponseEntity.success(attachFile);
    }

    /**
     *
     * @param file
     * @param type 1证件号 2 银行卡号
     * @return
     */
    @PostMapping("/huaweiOcr")
    @Operation(summary =  "证件orc")
    public ServerResponseEntity huaweiOcr(@RequestParam("file") MultipartFile file,String type) {
        return ServerResponseEntity.success(HuaweiOcr.huaweiOcrGetName(file,type));
    }

}
